package com.example.sunmiv1.activity.member;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.sunmiv1.R;
import com.example.sunmiv1.adapter.VipLabelListAdapter;
import com.example.sunmiv1.base.BaseActivity;
import com.example.sunmiv1.entity.GetUserLabelInfo;
import com.example.sunmiv1.entity.member.MemberLabelManagerInfo;
import com.example.sunmiv1.utils.Tools;
import com.example.sunmiv1.utils.net.Common;
import com.example.sunmiv1.utils.net.GsonUtils;
import com.example.sunmiv1.utils.net.HttpUtils;
import com.example.sunmiv1.utils.net.TextCallBack;
import com.example.sunmiv1.views.RecyclerViewNoScrollUtil;
import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexWrap;
import com.google.android.flexbox.JustifyContent;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class EditLabelActivity extends BaseActivity {

    @BindView(R.id.title_back)
    ImageView titleBack;
    @BindView(R.id.title_title)
    TextView titleTitle;
    @BindView(R.id.editLabel_search)
    ImageView editLabelSearch;
    @BindView(R.id.editLabel_edit)
    EditText editLabelEdit;
    @BindView(R.id.editLabel_list)
    RecyclerView editLabelList;
    @BindView(R.id.editLabel_save)
    TextView editLabelSave;

    private Context context;
    private MemberLabelManagerInfo memberLabelManagerInfo;
    private List<MemberLabelManagerInfo.DataBean.ListBean> listBeans = new ArrayList<>();
    private List<MemberLabelManagerInfo.DataBean.ListBean> temporaryList = new ArrayList<>();

    private VipLabelListAdapter vipLabelListAdapter;

    private int size = 0;

    private String vid = "";
    private List<GetUserLabelInfo.DataBean.ListBean> labelList;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    for (int i = 0; i < labelList.size(); i++) {
                        for (MemberLabelManagerInfo.DataBean.ListBean listBean : memberLabelManagerInfo.getData().getList()) {
                            if (labelList.get(i).getVip_label_id().equals(listBean.getId())) {
                                listBean.setCheck(true);
                                size++;
                            }
                        }
                    }
                    listBeans.addAll(memberLabelManagerInfo.getData().getList());
                    setVipLabelListAdapter();
                    break;
                case 2:
                    setVipLabelListAdapter();
                    break;
            }
        }
    };

    @Override
    protected void initView() {
        setContentView(R.layout.activity_edit_label);
        ButterKnife.bind(this);
        context = this;
        titleTitle.setText("添加標籤");
        RecyclerViewNoScrollUtil recyclerViewNoScrollUtil1 = new RecyclerViewNoScrollUtil(context);
        recyclerViewNoScrollUtil1.setScrollEnabled(false);
        recyclerViewNoScrollUtil1.setFlexDirection(FlexDirection.ROW);
        recyclerViewNoScrollUtil1.setFlexWrap(FlexWrap.WRAP);//按正常方向換行
        recyclerViewNoScrollUtil1.setJustifyContent(JustifyContent.FLEX_START);//交叉軸的起點對齊。
        editLabelList.setLayoutManager(recyclerViewNoScrollUtil1);
        setVipLabelListAdapter();
    }

    @Override
    protected void initData() {
        vid = getIntent().getStringExtra("vid");
        labelList = (List<GetUserLabelInfo.DataBean.ListBean>) getIntent().getSerializableExtra("list");
        getUserLabel();
    }

    @Override
    protected void initListener() {
        editLabelEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(editLabelEdit.getText().toString().trim())) {
                    listBeans.clear();
                    listBeans.addAll(temporaryList);
                    setVipLabelListAdapter();
                }
            }
        });
    }

    @OnClick({R.id.title_back, R.id.editLabel_search, R.id.editLabel_save})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_back:
                finish();
                break;
            case R.id.editLabel_search:
                if (TextUtils.isEmpty(editLabelEdit.getText().toString().trim())) {
                    Toast.makeText(context, "請輸入要搜索的會員標籤", Toast.LENGTH_SHORT).show();
                    return;
                }
                temporaryList.clear();
                temporaryList.addAll(listBeans);
                listBeans.clear();
                for (int i = 0; i < temporaryList.size(); i++) {
                    if (temporaryList.get(i).getName().contains(editLabelEdit.getText().toString().trim())) {
                        listBeans.add(temporaryList.get(i));
                    }
                }
                setVipLabelListAdapter();
                break;
            case R.id.editLabel_save:
                break;
        }
    }

    private void setVipLabelListAdapter() {
        if (vipLabelListAdapter == null) {
            vipLabelListAdapter = new VipLabelListAdapter(getIntent().getStringExtra("vid"), context, listBeans);
            editLabelList.setAdapter(vipLabelListAdapter);
        } else {
            vipLabelListAdapter.notifyDataSetChanged();
        }
        vipLabelListAdapter.setSize(size);
    }

    /**
     * 獲取會員標籤
     */
    private void getUserLabel() {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("page", 1);
            jsonObject.put("pageSize", 100000);
            jsonObject = Tools.createParameterCode(jsonObject);
            HttpUtils.post(context, Common.GET_VIP_LABEL, jsonObject, new TextCallBack() {
                @Override
                protected void onSuccess(String text) {
                    memberLabelManagerInfo = GsonUtils.JsonToBean(text, MemberLabelManagerInfo.class);
                    Message msg = new Message();
                    if (memberLabelManagerInfo.getStatus() == 1) {
                        msg.what = 1;
                    } else {
                        msg.what = 2;
                    }
                    handler.sendMessage(msg);
                }

                @Override
                protected void onFailure(ResponseException e) {

                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

}
